<template>
  <div class="app-container">
    <!-- 设置对话框 -->
    <el-dialog :close-on-click-modal="false" title="手环设置" :visible.sync="show" @on-cancel="show = false" width="1000px"
      @open="onOpen" append-to-body>
      <el-form :model="settingForm" ref="settingForm" :inline="true" label-width="180px">
        <el-form-item label="计步器开关" prop="stepswitch">
          <el-switch v-model="settingForm.stepswitch" :active-value="1" active-text="开" :inactive-value="0"
            inactive-text="关" style="width:200px"></el-switch>
        </el-form-item>
        <el-form-item label="跌倒报警开关" prop="falldown">
          <el-switch v-model="settingForm.falldown" :active-value="1" active-text="开" :inactive-value="0"
            inactive-text="关"></el-switch>
        </el-form-item>


        <el-form-item label="定位间隔时间（分钟）" prop="locationtimeinterval">
          <el-input type="number" v-model="settingForm.locationtimeinterval" placeholder="请输入定位间隔时间" clearable
            size="small" />
        </el-form-item>

        <el-form-item label="心率间隔时间（分钟）" prop="heartratetimeinterval">
          <el-input type="number" v-model="settingForm.heartratetimeinterval" placeholder="请输入心率间隔时间" clearable
            size="small" />
        </el-form-item>

        <el-form-item label="血压间隔时间（分钟）" prop="bpi">
          <el-input type="number" v-model="settingForm.bpi" placeholder="请输入血压间隔时间" clearable size="small" />
        </el-form-item>

        <el-form-item label="体温间隔时间（分钟）" prop="tpi">
          <el-input type="number" v-model="settingForm.tpi" placeholder="请输入体温间隔时间" clearable size="small" />
        </el-form-item>

        <el-form-item label="血氧间隔时间（分钟）" prop="oxi">
          <el-input type="number" v-model="settingForm.oxi" placeholder="请输入血氧间隔时间" clearable size="small" />
        </el-form-item>

        <el-form-item label="最大心率阀值" prop="maxheartrate">
          <el-input type="number" v-model="settingForm.maxheartrate" placeholder="请输入最大心率阀值" clearable size="small" />
        </el-form-item>

        <el-form-item label="最小心率阀值" prop="minheartrate">
          <el-input type="number" v-model="settingForm.minheartrate" placeholder="请输入最小心率阀值" clearable size="small" />
        </el-form-item>

        <el-form-item label="最大收缩压阀值（高压）" prop="bphighmax">
          <el-input type="number" v-model="settingForm.bphighmax" placeholder="请输入最大收缩压阀值（高压）" clearable size="small" />
        </el-form-item>

        <el-form-item label="最小收缩压阀值（高压）" prop="bphighmin">
          <el-input type="number" v-model="settingForm.bphighmin" placeholder="请输入最小收缩压阀值（高压）" clearable size="small" />
        </el-form-item>

        <el-form-item label="最大舒张压阀值（低压）" prop="bplowmax">
          <el-input type="number" v-model="settingForm.bplowmax" placeholder="请输入最大舒张压阀值（低压）" clearable size="small" />
        </el-form-item>

        <el-form-item label="最小舒张压阀值（低压）" prop="bplowmin">
          <el-input type="number" v-model="settingForm.bplowmin" placeholder="请输入最小舒张压阀值（低压）" clearable size="small" />
        </el-form-item>

        <el-form-item label="最大血氧阀值" prop="bloodoxmax">
          <el-input type="number" v-model="settingForm.bloodoxmax" placeholder="请输入最大血氧阀值" clearable size="small" />
        </el-form-item>

        <el-form-item label="最小血氧阀值" prop="bloodoxmin">
          <el-input type="number" v-model="settingForm.bloodoxmin" placeholder="请输入最小血氧阀值" clearable size="small" />
        </el-form-item>

        <el-form-item label="最大体温" prop="tempmax">
          <el-input type="number" v-model="settingForm.tempmax" placeholder="请输入最大体温" clearable size="small" />
        </el-form-item>

        <el-form-item label="最小体温" prop="tempmin">
          <el-input type="number" v-model="settingForm.tempmin" placeholder="请输入最小体温" clearable size="small" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" icon="el-icon-setting" :loading="btnLoading"
          @click="handleSetting()">提交设置</el-button>
        <el-button @click="show = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { setDevice } from "@/api/base/device";


export default {
  name: "SetDevice",
  props: {
    imeis: {
      type: Array,
      default: function () {
        return [];
      },
    },
    visible: {
      type: Boolean,
      default: function () {
        return false;
      },
    },

  },
  data() {
    return {
      // 表单参数
      settingForm: {},
      // 表单校验
      rules: {


      },
      btnLoading: false,
    };
  },
  computed: {
    show: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit("update:visible", val);
      },
    },
  },
  created() {
  },
  methods: {
    resetForm() {
      this.settingForm = {
        stepswitch: 1, // 默认值
        locationtimeinterval: 1,
        heartratetimeinterval: 60,
        bpi: 60,
        tpi: 60,
        oxi: 60,
        maxheartrate: 120,
        minheartrate: 60,
        bphighmax: 140,
        bphighmin: 100,
        bplowmax: 90,
        bplowmin: 60,
        bloodoxmax: 100,
        bloodoxmin: 80,
        tempmax: 39,
        tempmin: 35,
        falldown: 1, // 默认值
      }
    },

    /**设置按钮操作 */
    handleSetting() {
      this.btnLoading = true;
      let params = JSON.parse(JSON.stringify(this.settingForm));
      for (const key in params) {
        if (params.hasOwnProperty(key) && params[key] === "") {
          delete params[key];
        }
      }
      setDevice({
        deviceIds: this.imeis.join(','),
        params: params,
      }).then((response) => {
        if (response.code === 200) {
          this.$modal.msgSuccess(response.msg);
          this.$emit("refreshList");
          this.show = false;
          this.btnLoading = false;
        }
      }).catch((error) => {
        this.btnLoading = false;
      });
    },

    onOpen() {

      this.resetForm();
    },
  },



};
</script>
